package com.wanmait.teatrain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanmait.teatrain.dto.QuestionsDTO;
import com.wanmait.teatrain.entity.*;
import com.wanmait.teatrain.mapper.AdminPaperMapper;
import com.wanmait.teatrain.mapper.CourseResourceMapper;
import com.wanmait.teatrain.mapper.PaperSetMapper;
import com.wanmait.teatrain.mapper.QuestionsMapper;
import com.wanmait.teatrain.service.AdminPaperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.teatrain.service.CourseResourceService;
import com.wanmait.teatrain.utils.AjaxResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户考试-试卷表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2025-10-22
 */
@Service
public class AdminPaperServiceImpl extends ServiceImpl<AdminPaperMapper, AdminPaper> implements AdminPaperService {

    @Resource
    private AdminPaperMapper adminPaperMapper;
    @Resource
    private PaperSetMapper paperSetMapper;
    @Resource
    private CourseResourceMapper courseResourceMapper;
    @Resource
    private QuestionsMapper questionsMapper;

    //组卷功能测试 --徐钰淞
    @Override
    public AjaxResult getPaper(Integer courseId, Integer adminId, Boolean isSimulate) {
        //获得考试策略
        QueryWrapper<PaperSet> papetSet  = new QueryWrapper<>();
        papetSet.eq("course_id", courseId);
        List<PaperSet> paperSets = paperSetMapper.selectList(papetSet);
        Integer paperSetId = paperSets.get(0).getId();
        //添加用户考试-试卷表
        AdminPaper adminPaper = new AdminPaper();
        adminPaper.setAdminId(adminId);
        adminPaper.setPaperSetId(paperSetId);
        adminPaper.setIsSimulate(isSimulate);
        adminPaper.setStartTime(new Date());
        adminPaper.setStatus(2);
        //adminPaperMapper.insert(adminPaper);
        //从数据库抽取题目
        QuestionsDTO questions = getQuestions(paperSets.get(0));
        //随机题目
        questions = getRandomQuestions(paperSets.get(0), questions);
        //固定题目
        //getDefiniteQuestions(paperSets.get(0),questions);

        Map<Questions,List<Options>> paperQuestions = new HashMap<>();
        


        return null;
    }

    //获得题目(根据资源id) --徐钰淞
    private QuestionsDTO getQuestions(PaperSet paperSet) {
        Integer courseId = paperSet.getCourseId();
        //获得课程资源列表
        QueryWrapper<CourseResource> queryResource = new QueryWrapper<CourseResource>().eq("course_id", courseId);
        List<CourseResource> courseResources = courseResourceMapper.selectList(queryResource);
        List<Questions> questionList = new ArrayList<>();
        List<Integer> resourceIds = courseResources.stream()
                .map(CourseResource::getResourceId)
                .collect(Collectors.toList());
        QueryWrapper<Questions> choiceQuestion = new QueryWrapper<Questions>();
        QueryWrapper<Questions> judgeQuestion = new QueryWrapper<Questions>();
        QueryWrapper<Questions> answerQuestion = new QueryWrapper<Questions>();
        choiceQuestion.eq("question_type", 1)
                .in("resource_id", resourceIds);
        judgeQuestion.eq("question_type",2)
                .in("resource_id", resourceIds);
        answerQuestion.eq("question_type",3)
                .in("resource_id", resourceIds);
        List<Questions> choiceQuestions = new ArrayList<>(questionsMapper.selectList(choiceQuestion));
        List<Questions> judgeQuestions = new ArrayList<>(questionsMapper.selectList(judgeQuestion));
        List<Questions> answerQuestions = new ArrayList<>(questionsMapper.selectList(answerQuestion));
        return new QuestionsDTO(choiceQuestions, judgeQuestions, answerQuestions);
    }

    //获得随机试题 --徐钰淞
    private QuestionsDTO getRandomQuestions(PaperSet paperSet, QuestionsDTO questionsDTO) {
        System.out.println("获得随机试题的方法");
        Integer choiceNum = paperSet.getSingleChoiceCount();
        Integer judgeNum = paperSet.getJudgmentCount();
        Integer answerNum = paperSet.getShortAnswerCount();

        // 边界校验：n必须在[0, list.size()]范围内
        if (choiceNum < 0 || choiceNum > questionsDTO.getChoiceQuestions().size()) {
            throw new IllegalArgumentException("单选题数量不足");
        }
        if (judgeNum < 0 || judgeNum > questionsDTO.getJudgeQuestions().size()) {
            throw new IllegalArgumentException("判断题数量不足");
        }
        if (answerNum < 0 || answerNum > questionsDTO.getAnswerQuestions().size()) {
            throw new IllegalArgumentException("简答题数量不足");
        }
        List<Questions> newList = new ArrayList<>(questionsDTO.getChoiceQuestions());
        Collections.shuffle(newList);
        newList = newList.stream().limit(choiceNum).collect(Collectors.toList());
        questionsDTO.setChoiceQuestions(newList);

        newList = new ArrayList<>(questionsDTO.getJudgeQuestions());
        Collections.shuffle(newList);
        newList = newList.stream().limit(judgeNum).collect(Collectors.toList());
        questionsDTO.setJudgeQuestions(newList);

        newList = new ArrayList<>(questionsDTO.getAnswerQuestions());
        Collections.shuffle(newList);
        newList = newList.stream().limit(answerNum).collect(Collectors.toList());
        questionsDTO.setAnswerQuestions(newList);

        return questionsDTO;
    }
    //获得固定试题 --徐钰淞
    private void getDefiniteQuestions(PaperSet paperSet, QuestionsDTO questionsDTO) {

    }
}
